﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace DroughtAnalysis
{
    public class ScriptTemplateReader
    {
        /// <summary>
        /// Tries to read the script template from the file in the same
        /// folder as this assembly. If not found, then use the script
        /// template from the DLL.
        /// </summary>
        /// <param name="templateName">the script template name</param>
        /// <returns>the contens of the script template</returns>
        public string GetScriptTemplate(string templateScriptPath)
        {
            if (File.Exists(templateScriptPath))
            {
                return File.ReadAllText(templateScriptPath);
            }
            else
            {
                return GetFromResources("DroughtAnalysis." + Path.GetFileName(templateScriptPath));
            }
        }
        
        
        private string GetFromResources(string resourceName)
        {
            Assembly assem = this.GetType().Assembly;
            using (Stream stream = assem.GetManifestResourceStream(resourceName))
            {
                using (var reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
}
